<?php
use GatewayClient\Gateway;

class controller_home extends model_home
{
    //首页
    public function web()
    {
        //下单前判断redis队列库存量
        //$redis = new Redis();
        //$result=$redis->connect('127.0.0.1',6379);
        //var_dump($result);
        $this->display("home/index");
    }

    public function leftmenu()
    {
        $advs_category = $this->mod_advs_category();
        $this->display('iframe/leftmenu', array(
            'advs_category' => $advs_category,
        ));
    }

    public function desktop()
    {
        $this->display('iframe/desktop');
    }

    public function tip()
    {
        //$userlist = Gateway::getClientSessionsByGroup("public");
        $userlist = Gateway::getAllClientSessions();
        $publicuserlist = Gateway::getClientSessionsByGroup("public");
        array_unique($publicuserlist, SORT_REGULAR);
        if( $publicuserlist ){
            foreach($publicuserlist as $k=>$v){
                if(isset($v['uid'])) {
                    $publicuserlist = $this->array_unset_tt($publicuserlist, 'uid', $v['uid']);
                }
            }
        }

        $result = $this->mod_tip();
        $result['online'] = $userlist = count($publicuserlist) . '/' . count($userlist);
        echo json_encode($result);
    }

    public function getRandom($array)
    {
        $numAll = 1000;
        $res = 0;
        //中奖概率基数
        $num = array_sum($array) * $numAll;//当前一等奖概率1/10000

        foreach ($array as $k => $v) {
            $v = $v * $numAll;
            //获取一个1到当前基数范围的随机数
            $rand = mt_rand(1, $num);
            if ($rand <= $v) {
                //假设当前奖项$k=2,$v<=5才能中奖
                $res = $k;
                break;
            } else {
                //假设当前奖项$k=6,$v>1900,则没中六等奖,总获奖基数2000-1900,前五次循环都没中则2000-1-5-10-24-60=1900,必中6等奖,哈哈
                $num -= $v;
            }
        }
        return $res;
    }
	
}